<?php

class DefaultController extends Controller {
	
	public function actionIndex() {
		$this->layout = AdminModule::COMMON_LAYOUT.'main';
		$this->render('index');
	}
	
	public function actionTest() {
		$this->layout = AdminModule::COMMON_LAYOUT.'main';
		$this->render('test');
	}
	
	public function actionError() {
		$this->layout = AdminModule::COMMON_LAYOUT.'main';
		if($error=Yii::app()->errorHandler->error) {
	        $this->render('error', $error);
	    } else {
	    	$this->redirect($this->createUrl(Yii::app()->adminUser->loginUrl));
	    }
	}
	
	public function actionLogin() {
		if(!Yii::app()->adminUser->isGuest) {
			$this->redirect(Yii::app()->adminUser->returnUrl);
		}
		$model = new AdminLoginForm();

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['AdminLoginForm']))
		{
			$model->attributes=$_POST['AdminLoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()) {
				Yii::app()->user->setFlash(FLASH::RESULT_INFO, '欢迎您，您已成功登录系统！');
				$this->redirect(Yii::app()->adminUser->returnUrl);
			}
		}
		
		$this->layout = AdminModule::COMMON_LAYOUT.'noframe';
		Yii::app()->user->setFlash(FLASH::RESULT_INFO, '请输入用户名和密码.');
		
		// display the login form
		$this->render('login',array('model'=>$model));
		
	}
	
	public function actionLogout() {
		Yii::app()->adminUser->logout();
		$this->redirect($this->createUrl(Yii::app()->adminUser->loginUrl));
	}
	public function actions() {
		return array(
			'updatePwd' => array(
				'class' => AdminModule::ACTIONS.'ChangePwdAction',
				'view' => AdminModule::COMMON_VIEW.'update_pwd',
				'memberModel' => empty(Yii::app()->adminUser->groupId) ? Yii::app()->adminUser->role : 'GroupAccount',
				'memberId' => Yii::app()->adminUser->id,
				'returnUrl' => array('index'),
				'layout' => AdminModule::COMMON_LAYOUT.'column1',
			),
		);
	}
}

?>